package com.hqyj.emb.system.controller;

import com.hqyj.emb.system.model.Permission;
import com.hqyj.emb.system.model.Role;
import com.hqyj.emb.system.model.RolePermission;
import com.hqyj.emb.system.model.User;
import frame.controller.BaseController2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/permissionController.do")
public class PermissionController extends BaseController2 {



    public String list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Permission> permissionList_db = permissionService.queryAllRolePermission();
        req.setAttribute("permissionList_db", permissionList_db);
        return "view/system/permission/permissionList.jsp";
    }

    public String addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Permission> permissions_db = permissionService.queryAllRolePermission();
        req.setAttribute("permissions_db", permissions_db);
        return "view/system/permission/addPermission.jsp";
    }

    public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String pIdSTR = req.getParameter("pIdSTR");
        int pId = Integer.parseInt(pIdSTR);
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        Permission permission = new Permission();
        permission.setpId(pId);
        permission.setName(name);
        permission.setType(type);
        permission.setUrl(url);
        permission.setState(state);
        int n = permissionService.updateRolePermission(permission);
        List<Permission> permissionList_db = permissionService.queryAllRolePermission();
        req.setAttribute("permissionList_db", permissionList_db);
        return "view/system/permission/permissionList.jsp";
    }

    public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String permissionIdSTR = req.getParameter("permissionIdSTR");
        int permissionId = Integer.parseInt(permissionIdSTR);
        int n = permissionService.deleteRolePermission(permissionId);
        List<Permission> permissionList_db = permissionService.queryAllRolePermission();
        req.setAttribute("permissionList_db", permissionList_db);
        return "view/system/permission/permissionList.jsp";
    }

    public String updateUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String permissionIdSTR = req.getParameter("permissionIdSTR");
        int permissionId = Integer.parseInt(permissionIdSTR);
        Permission permission_db = permissionService.queryRolePermission(permissionId);

        List<Permission> permissionList_db = permissionService.queryAllRolePermission();
        req.setAttribute("permissionList_db", permissionList_db);

        req.setAttribute("permission_db", permission_db);
        return "view/system/permission/updatePermission.jsp";
    }

    public String update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String permissionIdSTR = req.getParameter("permissionIdSTR");
        int permissionId = Integer.parseInt(permissionIdSTR);
        String pIdSTR = req.getParameter("pIdSTR");
        int pId = Integer.parseInt(pIdSTR);
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        Permission permission = new Permission();
        permission.setPermissionId(permissionId);
        permission.setpId(pId);
        permission.setName(name);
        permission.setType(type);
        permission.setUrl(url);
        permission.setState(state);
        int n = permissionService.updatePermission(permission);


        List<Permission> permissionList_db = permissionService.queryAllRolePermission();
        req.setAttribute("permissionList_db", permissionList_db);
        return "view/system/permission/permissionList.jsp";
    }

    public String FPPermissionUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleIdSTR = req.getParameter("roleIdSTR");
        int roleId = Integer.parseInt(roleIdSTR);
        Role role_db = roleService.queryRoleByRoleId(roleId);
        List<Permission> permissions_db = permissionService.queryAllRolePermission();
        List<Permission> permission_xz = permissionService.queryRolePermissionByRoleId(roleId);


        List<Integer> list = new ArrayList<Integer>();
        for (Permission permission : permission_xz) {
            list.add(permission.getPermissionId());
        }
        req.setAttribute("ids", list);
        req.setAttribute("permissions_db", permissions_db);
        req.setAttribute("role_db", role_db);
        return "view/system/permission/FPPermission.jsp";
    }
    public String FPPermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleIdSTR = req.getParameter("roleIdSTR");
        int roleId = Integer.parseInt(roleIdSTR);


        String[] permissionIdSTRS = req.getParameterValues("permissionIdSTR");
        int n=rolePermissionService.deleteRolePermissionByRoleId(roleId);
        RolePermission rolePermission=new RolePermission();
        for (String permissionIdSTR : permissionIdSTRS) {
            int permissionId=Integer.parseInt(permissionIdSTR);
            rolePermission.setPermissionId(permissionId);
            rolePermission.setRoleId(roleId);

            int k =rolePermissionService.addRolePermissionByPermissionId(rolePermission);
        }

        Role role_db = roleService.queryRoleByRoleId(roleId);
        List<Permission> permissions_db = permissionService.queryAllRolePermission();
        List<Permission> permission_xz = permissionService.queryRolePermissionByRoleId(roleId);

        List<Integer> list = new ArrayList<Integer>();
        for (Permission permission : permission_xz) {
            list.add(permission.getPermissionId());
        }
        req.setAttribute("message", "分配权限成功！继续分配吗？");

        req.setAttribute("ids", list);
        req.setAttribute("permissions_db", permissions_db);
        req.setAttribute("role_db", role_db);
        return "view/system/permission/FPPermission.jsp";
    }
    public String PLDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String checkId = req.getParameter("checkId");
        System.out.println(checkId);
        String[] split = checkId.split(",");
        int[] ids=new int[split.length];
        if(split.length>0){
            for(int i=0;i<split.length;i++){
                ids[i]=Integer.parseInt(split[i]);
            }
        }
        for (int id : ids) {
            int i = permissionService.deleteRolePermission(id);
        }
        List<Permission> permissionList_db = permissionService.queryAllRolePermission();
        req.setAttribute("permissionList_db", permissionList_db);
        return "view/system/permission/permissionList.jsp";
    }
}
